<?php
/**
 * @version		$Id: edit.php $
 * @package		Joomla.Administrator
 * @subpackage	com_gencode
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<script type="text/javascript">
window.addEvent('domready', function(){
	var el = document.id('ajax-form');
	 
	var request = new Request({
	    url: '<?php echo JRoute::_('index.php?option=com_gencode&view=genm&tmpl=component', false)?>',
	    method: 'get',
	    onRequest: function(){ el.set('text', 'loading...'); },
	    onSuccess: function(responseHTML){
		    el.set('html', responseHTML);

		    var elTableFields = document.id('list-table-fields');
			
			var requestGetTable = new Request({
			    url: '<?php echo JRoute::_('index.php?option=com_gencode&view=genm&tmpl=component', false)?>',
			    method: 'get',
			    onRequest: function(){ elTableFields.set('text', 'loading...'); },
			    onSuccess: function(responseText){
			    	elTableFields.set('html', responseText);

			    	//TODO: Must moved this snippet to genm_form.js
			    	if(document.id('jform_type').get('value') == 'form')
			    	{
				    	<?php
		    			$i = 0;
		    			foreach ($this->item->form_fields as $formField): 
		    			?>
	
		    			var container_<?php echo $i; ?> = $('field_<?php echo $i; ?>').getNext('span');
		    			getFieldTypeConfig(container_<?php echo $i; ?>, document.id('field_<?php echo $i; ?>').get('value'), document.id('field_<?php echo $i; ?>').get('field'));
	
		    			<?php 
		    			$i ++;
		    			endforeach; 
		    			?>
			    	}
			    	//END
			    },
			    onFailure: function(){ elTableFields.set('html', 'Failure !'); }
			});

			if(document.id('jform_type').get('value') == 'form')
				var layout = 'form_elements';
			else
				var layout = 'table_fields';

		    //add event onChange to combobox select table
		    document.id('list-select-table').addEvent('change:relay(select)', function(){
				requestGetTable.send('layout='+layout+'&table_id=' + this.get('value') + '&id=<?php echo $this->item->id; ?>' );
			});

		    if($$('#list-select-table select').get('value') != '')
				requestGetTable.send('layout='+layout+'&table_id=' + document.id('table_id').get('value') + '&id=<?php echo $this->item->id; ?>' );
			
		    /* for list */	
		    if(document.getElementById('default-list'))
		    {	    
				<?php require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'html' . DS . 'js' . DS . 'genm_list.js.php'; ?>
		    }
		    /* end for list */

			/* for form */
			 if(document.getElementById('default-form'))
		    {
		    	<?php require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'html' . DS . 'js' . DS . 'genm_form.js.php'; ?>
		    }
			/* end for form */
	    },
	    onFailure: function(){ el.set('html', ''); }
	});
	 
	document.id('jform_type').addEvent('change', function(event){
	    event.stop();
	    var value = this.get('value');

	    if(value == '')
	    {
		    el.set('text', '');
		    return false;
	    }
	    
	    request.send('tpl=' + value + '&mid=<?php echo $this->item->id; ?>&id=<?php echo $this->item->id; ?>' );
	});

	//if(document.id('jform_type').get('value') != '')
	//{
		request.send('tpl=' + document.id('jform_type').get('value') + '&mid=<?php echo $this->item->id; ?>&id=<?php echo $this->item->id; ?>' );
	//}
});

function getFieldTypeConfig(container, field_type, field_name)
{
	var requestFields = new Request({
	    url: '<?php echo JRoute::_('index.php?option=com_gencode&task=genm.getConfig&genm_id='.$this->item->id.'&table_id=', false)?>' + document.id('table_id').get('value'),
	    method: 'get',
	    onRequest: function(){ container.set('html', 'Loading ...'); },
	    onSuccess: function(responseText){
	    	container.set('html', responseText);
	    },
	    onFailure: function(){ fields.set('html', 'Failure !'); }
	});

	requestFields.send('field_type=' + field_type + '&field_name=' + field_name);
}

function changeFields(fields, value, select_ordering, selected_value)
{
	var requestFields = new Request({
	    url: '<?php echo JRoute::_('index.php?option=com_gencode&view=genm&tmpl=component', false)?>',
	    method: 'get',
	    onRequest: function(){ fields.set('html', '<select class="width-110"><option>loading</option></select>'); },
	    onSuccess: function(responseText){
	    	fields.set('html', responseText);
	    },
	    onFailure: function(){ fields.set('html', 'Failure !'); }
	});

	requestFields.send('layout=fields&table=' + value + '&select_ordering=' + select_ordering + '&selected_value=' + selected_value);
}


	Joomla.submitbutton = function(task)
	{
		if (task == 'genm.cancel' || document.formvalidator.isValid(document.id('genm-form'))) {
			Joomla.submitform(task, document.getElementById('genm-form'));
		}
	}
</script>

<form action="<?php echo JRoute::_('index.php?option=com_gencode&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="genm-form" class="form-validate">
	<div class="width-100 fltlft">
		<fieldset class="adminform">
			<legend><?php echo empty($this->item->id) ? JText::_('COM_GENCODE_NEW_GENM') : JText::sprintf('COM_GENCODE_GENM_DETAILS', $this->item->id); ?></legend>
			<ul class="adminformlist">
				<li><?php echo $this->form->getLabel('name'); ?>
				<?php echo $this->form->getInput('name'); ?></li>

				<li><?php echo $this->form->getLabel('type'); ?>
				<?php echo $this->form->getInput('type'); ?></li>

				<li><?php echo $this->form->getLabel('description'); ?>
				<?php echo $this->form->getInput('description'); ?></li>
				
				<?php foreach($this->form->getFieldset('publish') as $field): ?>
				<li><?php echo $field->label; ?>
					<?php echo $field->input; ?></li>
				<?php endforeach; ?>

				<li><?php echo $this->form->getLabel('id'); ?>
				<?php echo $this->form->getInput('id'); ?></li>
			</ul>
			<div class="clr"> </div>
			
			<div id="ajax-form"></div>
			
			<div class="clr"> </div>

		</fieldset>
	</div>
	<input type="hidden" name="task" value="" />
	<?php echo JHtml::_('form.token'); ?>
	<div class="clr"></div>
</form>
